<?php 
namespace App\Services;

use App\Facades\Infra\CaseManager;
use Auth;
use App\Facades\Utils\ValidateParameter;
use App\Facades\Utils\LogUtil;
use App\Exceptions\ValidateParameterException;
use App\Models\Sets;
Use Log;

class CaseService{

	public function getAll($request){

		$conditions = $request->all();

		return CaseManager::getCases($conditions);
	}

	public function getCaseFrontByJianZhong($request){

		$data = $request->all();

		$set = Sets::find(1);

		$conditions['case_type'] = '肥胖症';

		$conditions = $this->formConditions($conditions,$data,$set);

		return CaseManager::getCaseFront($conditions);
	}

	public function getCaseFrontByTangNiaoBing($request){

		$data = $request->all();

		$set = Sets::find(1);

		$conditions['case_type'] = '糖尿病';

		$conditions = $this->formConditions($conditions,$data,$set);

		return CaseManager::getCaseFront($conditions);
	}

	public function formConditions($conditions,$data,$set){

		if(isset($data['age']) && $data['age'] != '' && $data['age'] != null){
			$conditions['start_age'] = $data['age'] - $set['age_section'];
			$conditions['end_age'] = $data['age'] + $set['age_section'];
		}

		if(isset($data['weight']) && $data['weight'] != '' && $data['weight'] != null){
			$conditions['start_weight'] = $data['weight'] - $set['weight_section'];
			$conditions['end_weight'] = $data['weight'] + $set['weight_section'];
		}

		if(isset($data['bmi']) && $data['bmi'] != '' && $data['bmi'] != null){
			$conditions['start_bmi'] = $data['bmi'] - $set['bmi_section'];
			$conditions['end_bmi'] = $data['bmi'] + $set['bmi_section'];
		}

		if(isset($data['sex']) && $data['sex'] != '' && $data['sex'] != null){
			$conditions['sex'] = $data['sex'];
		}

		if(isset($data['province_id']) && $data['province_id'] != '' && $data['province_id'] != null){
			$conditions['province_id'] = $data['province_id'];
		}

		if(isset($data['history']) && $data['history'] != '' && $data['history'] != null){
			$conditions['history'] = $data['history'];
		}


		return $conditions;
	}

	public function getById($request){
		$rules = [
			'id' => 'required|exists:cases,id'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id = $data['id'];

		return CaseManager::getCase($id);
	}

	public function create($request){
		$rules = [
			'case_type' => 'required|exists:case_types,type_name',
			'title' => 'required',
			'desc' => 'required',
			'name' => 'required',
			'age' => 'required|numeric',
			'sex' => 'required|boolean',
			'province_id' => 'required|exists:provinces,id',
			'city_id' => 'required|exists:citys,id',
			'manner' => 'required',
			'time' => 'required|date',
			'height' => 'required|numeric',
			'introduce' => 'required',
			'picture' => 'required',
			'f_weight' => 'required|numeric',
			'f_bmi' => 'required|numeric',
			'f_waistline' => 'required',
			'f_hipline' => 'required',
			'f_picture' => 'required',
			'f_level' => 'required',
			'r_weight' => 'required|numeric',
			'r_bmi' => 'required|numeric',
			'r_waistline' => 'required',
			'r_hipline' => 'required',
			'r_picture' => 'required',
			'r_level' => 'required'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		// $data['introduce'] = base64_encode($data['introduce']);

		CaseManager::createCase($this->groupCase($data),$this->groupFront($data),$this->groupRear($data));

		LogUtil::createLog(Auth::user()->name,'创建了一个新案例：'.$data['name']);

	}

	public function update($request){

		$rules = [
			'id' => 'required|exists:cases,id',
			'case_type' => 'required|exists:case_types,type_name',
			'title' => 'required',
			'desc' => 'required',
			'name' => 'required',
			'age' => 'required|numeric',
			'sex' => 'required|boolean',
			'province_id' => 'required|exists:provinces,id',
			'city_id' => 'required|exists:citys,id',
			'manner' => 'required',
			'time' => 'required|date',
			'height' => 'required|numeric',
			'introduce' => 'required',
			'f_weight' => 'required|numeric',
			'f_bmi' => 'required|numeric',
			'f_waistline' => 'required',
			'f_hipline' => 'required',
			'f_level' => 'required',
			'r_weight' => 'required|numeric',
			'r_bmi' => 'required|numeric',
			'r_waistline' => 'required',
			'r_hipline' => 'required',
			'r_level' => 'required'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id=$data['id'];

		CaseManager::updateCase($id,$this->groupCase($data),$this->groupFront($data),$this->groupRear($data));

		LogUtil::updateLog(Auth::user()->name,'修改了一个案例：'.$data['name']);
	}

	public function delete($request){
		$rules = [
			'id' => 'required|exists:cases,id'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id=$data['id'];

		$case = CaseManager::deleteCase($id);

		LogUtil::updateLog(Auth::user()->name,'删除了一个案例：'.$case->name);
	}

	private function groupCase($data){

		if(!isset($data['history'])){
			throw new ValidateParameterException('字段异常');
		}
		$case = [
			'case_type' => $data['case_type'],
			'name' => $data['name'],
			'age' => $data['age'],
			'sex' => $data['sex'],
			'province_id' => $data['province_id'],
			'city_id' => $data['city_id'],
			'manner' => $data['manner'],
			'time' => $data['time'],
			'height' => $data['height'],
			'introduce' => $data['introduce'],
			'title' => $data['title'],
			'desc' => $data['desc'],
			'history' => $data['history'],
			'user_name' => Auth::user()->name
		];

		if(isset($data['picture']) && $data['picture']!='' && $data['picture']!=null){
			$case['picture'] = $data['picture'];
		}

		return $case;
	}

	private function groupFront($data){

		if(!isset($data['f_gi'])){
			throw new ValidateParameterException('字段异常');
		}

		$front = [
			'weight' => $data['f_weight'],
			'bmi' => $data['f_bmi'],
			'gi' => $data['f_gi'],
			'weight' => $data['f_weight'],
			'waistline' => $data['f_waistline'],
			'hipline' => $data['f_hipline'],
			'level_id' => $data['f_level']
		];

		if(isset($data['f_picture']) && $data['f_picture']!='' && $data['f_picture']!=null){
			$front['picture'] = $data['f_picture'];
		}

		return $front;
	}

	private function groupRear($data){

		if(!isset($data['r_gi'])){
			throw new ValidateParameterException('字段异常');
		}

		$rear = [
			'weight' => $data['r_weight'],
			'bmi' => $data['r_bmi'],
			'gi' => $data['r_gi'],
			'weight' => $data['r_weight'],
			'waistline' => $data['r_waistline'],
			'hipline' => $data['r_hipline'],
			'picture' => $data['r_picture'],
			'level_id' => $data['r_level']
		];

		if(isset($data['r_picture']) && $data['r_picture']!='' && $data['r_picture']!=null){
			$rear['picture'] = $data['r_picture'];
		}

		return $rear;
	}
}